package com.gxa.user.handler;

import com.gxa.stu.common.BizException;
import com.gxa.stu.common.ExceptionCode;
import com.gxa.stu.common.JsonResult;
import com.gxa.stu.util.ResponeUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Throwable.class)
    public JsonResult handle(Throwable throwable){
        // 针对业务异常
        if(throwable instanceof BizException){
            String code = ((BizException) throwable).getExceptionCode().getCode();
            String message = ((BizException) throwable).getExceptionCode().getMessage();
            return ResponeUtil.failed(code,message);
        }
//        else if (throwable instanceof UnauthorizedException){
//            Integer code=8848;
//            String message="没有权限操作";
//            return ResponseUtil.failed(code,message);
//        }
        else{
            System.out.println("throwable:"+throwable);
            throwable.printStackTrace();
            ExceptionCode systemError = ExceptionCode. WRONG_SERVER;
            return ResponeUtil.failed(systemError.getCode(),systemError.getMessage());
        }
    }
}
